//https://liiked.github.io/VS-Code-Extension-Doc-ZH
// The module 'vscode' contains the VS Code extensibility API
import * as vscode from 'vscode';
import { TreeViewProvider } from './TreeViewProviderN';
import { createWebView } from './WebView';
import * as process from 'child_process';
// global.vscode = vscode;
export function activate(context: vscode.ExtensionContext) {

	//测试
	context.subscriptions.push(vscode.commands.registerCommand('lp-pro-tools.helloWorld', () => {
		vscode.window.showInformationMessage('Hello World!');
	}));

	context.subscriptions.push(vscode.commands.registerCommand('lp-pro-tools.loadProject', () => {
		TreeViewProvider.initTreeViewItem();
	}));
	
	//调入配置文件
	TreeViewProvider.initTreeViewItem();
	context.subscriptions.push(vscode.commands.registerCommand('itemClick', (treeItem , label ) => {
		vscode.window.showInformationMessage(label);
		let options = treeItem.options;
		if(options && options.file && options.file.path){
			vscode.workspace.openTextDocument(options.file.path)
				.then(doc => {
					// 在VSCode编辑窗口展示读取到的文本
					vscode.window.showTextDocument(doc , vscode.ViewColumn.One)
					.then((textEditor)=>{
                        let startPos = new vscode.Position(options.line-1, 0);
                        let endPos = new vscode.Position(options.line-1, 100);
						var newSelection = new vscode.Selection(startPos, endPos);
						textEditor && (textEditor.selection = newSelection);
						textEditor?.revealRange(newSelection, vscode.TextEditorRevealType.InCenter);
					});
				}, err => {
					vscode.window.showWarningMessage(`打开文件${options.file.path}错误,${err}`);
					// console.log(`Open ${options.file.path} error, ${err}.`);
				}).then(undefined, err => {
					vscode.window.showWarningMessage(`打开文件${options.file.path}错误,${err}`);
					// console.log(`Open ${options.file.path} error, ${err}.`);
				});
		}else if( options && options.url ){

			var cmd = `start ${options.url}`;
			process.exec(cmd, function(error, stdout, stderr) {
				console.log("error:"+error);
				console.log("stdout:"+stdout);
				console.log("stderr:"+stderr);
			});

			// const webView = createWebView(context, vscode.ViewColumn.Active, options.url);
			// context.subscriptions.push(webView);
		}
	}));
}

//@LINKS: 程序3入口
export function deactivate() {}
